﻿using System.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration.CommandLine;

namespace 配置系统_Json文件配置;

class Program
{
    static void Main(string[] args)
    {
        ConfigurationBuilder cb = new ConfigurationBuilder();
        
        
        /*
        // string name = configRoot.GetSection("Logging:LogLevel:Default").Value;
        // Console.WriteLine(name);
        //映射指定对象db
        var myDb = configRoot.GetSection("DB").Get<DB>();
        Console.WriteLine(myDb);
        //映射指定对象smtp
        var mySmtp = configRoot.GetSection("SMTP").Get<Smtp>();
        Console.WriteLine(mySmtp);
        //映射整个配置
        var myConfig = configRoot.Get<Config>();
        Console.WriteLine(myConfig);
        */
        /*通过依赖注入读取json配置
            var configRoot = cb.Build();
            cb.AddJsonFile("Config.Json", optional: false, reloadOnChange: true);
            ServiceCollection service = new ServiceCollection(); //创建DI容器
            service.AddScoped<ConfigController>(); //注册configController
            service.AddScoped<SMTPConfig>();
            service.AddOptions()
                .Configure<Config>(c => configRoot.Bind(c))
                .Configure<Smtp>(s => configRoot.GetSection("SMTP").Bind(s)); //绑定config并注册
            using (var sp = service.BuildServiceProvider())
            {
                while (true)
                {
                    string value = "";
                    using (var scope = sp.CreateScope())
                    {
                        var myconfig = scope.ServiceProvider.GetRequiredService<ConfigController>();
                        value = myconfig.GetConfigValueTest();
                    }

                    Console.WriteLine(value);
                    Console.WriteLine("按任意键继续");
                    Console.ReadKey();
                }

            }*/
        
        /*读取命令行配置
        //加载配置
        cb.AddCommandLine(args);
        //构建配置根节点
        var commandConfigRoot = cb.Build();
        //创建依赖容器
        ServiceCollection service = new ServiceCollection();
        service.AddScoped<ConfigController>();
        service.AddOptions().Configure<Config>(config=>commandConfigRoot.Bind(config));
        
        using (var sp = service.BuildServiceProvider())
        {
            var configController = sp.GetRequiredService<ConfigController>();
            Console.WriteLine("smtpInfo:{0}",configController.GetConfigValueTest().Smtp);
        }*/
        
       
    }
}

class Config
{
    public Logging Logging { get; set; }
    public string AllowedHosts { get; set; }
    public Smtp Smtp { get; set; }
    public DB Db { get; set; }
    public string Name { get; set; }

    public override string ToString()
    {
        base.ToString();
        return
            $"logging:{Logging.LogLevel.Default};allowedHosts:{AllowedHosts};Smtp:{Smtp.ToString()};db:{Db.ToString()}";
    }
}

class Logging
{
    public LogLevel LogLevel { get; set; }
}

class LogLevel
{
    public string Default { get; set; }
}

class DB
{
    public string dbType { get; set; }
    public string connectionstring { get; set; }

    public override string ToString()
    {
        return $"type:{dbType},conStr:{connectionstring}";
    }
}

class Smtp
{
    public string server { get; set; }
    public string userName { get; set; }
    public string password { get; set; }

    public override string ToString()
    {
        return $"server:{server},userName:{userName},password:{password}";
    }
}